/*
**NCS signal module head-files sig.h
**This module provide signal processing API for NCS 
**NCS uses signals to communicate with other devs
**including NMS RXEMS RCST SIGEMS PP
**
**Copyright (C) 2016-xxxx andisat <www.andisat.com>
**
**Author: andisat
**Date:2016-1
**
*/

#ifndef __SIG__
#define __SIG__

#include "ncs_serv.h"
#include "ncs_cfg.h"
#include "event.h"

#define NCS_VERSION 1

#define BROADCAST_UDP_PORT 2790
#define PACKET_MAX_TYPE 20

#pragma pack(1)
struct tlv_hdr{
    char type;
    short len;
};

struct packet_ctl{
    short msg;
    int seq;
    int tries;
    int dip;
    int dport;
    char type;
    int devId;
    int len;
    struct event *retry_timerev;
};

struct packet_hdr{
    char ver;
    char srcType;
    short msg;
    int seq;
    int srcDevId;
    short len;
};

#pragma pack()

#define PACKET_HEAD_LEN sizeof(struct packet_hdr)
#define PACKET_PDU_MAX 1400
#define DEF_POLL_RCST_PORT 2701

/*The message types between NMS and NCS*/
#define __NMS_BROADCAST_IPPORT__  0X0100
#define __NCS2NMS_REPORT__   0X0103
#define __NCS2NMS_LOGON_REQ__ 0X0101
#define __NMS2NCS_LOGON_ACK__ 0X0102
#define __NMS2NCS_ADD_NETWORK_REQ__  0X0104
#define __NCS2NMS_ADD_NETWORK_ACK__  0X0105
#define __NMS2NCS_ADD_PP_REQ__  0X0106
#define __NCS2NMS_ADD_PP_ACK__  0X0107
#define __NMS2NCS_ADD_MODEM_REQ__    0X0108
#define __NCS2NMS_ADD_MODEM_ACK__  0X0109
#define __NMS2NCS_LOGOFF_REQ__  0X010A
#define __NCS2NMS_LOGOFF_ACK__   0X010B
#define __NMS2NCS_CARRIERLOCK_REQ__    0X010C
#define __NCS2NMS_CARRIERLOCK_ACK__    0X010D
#define __NMS2NCS_CARRIERLOCK_RELEASE_REQ__   0X010E
#define __NCS2NMS_CARRIERLOCK_RELEASE_ACK__   0X010F
#define __NMS2NCS_RELEASE_RESOURCE_REQ__   0X0110
#define __NCS2NMS_RELEASE_RESOURCE_ACK__   0X0111
#define __NMS2NCS_PARCFG_UPDATE_REQ__  0X0112
#define __NCS2NMS_PARCFG_UPDATE_ACK__   0X0113
#define __NMS2NCS_DEL_DEV_REQ__   0X0114
#define __NCS2NMS_DEL_DEV_ACK__   0X0115
#define __NMS2NCS_QUERY_RESOURCE_STATUS_REQ__  0X0116
#define __NMS2NCS_QUERY_RESOURCE_STATUS_ACK__  0X0117
#define __NMS2NCS_QUERY_MODEM_STATUS_REQ__  0X0118
#define __NMS2NCS_QUERY_MODEM_STATUS_ACK__  0X0119
#define __NMS2NCS_QUERY_LICENSE_INFO_REQ__  0X011A
#define __NMS2NCS_QUERY_LICENSE_INFO_ACK__   0X011B
#define __NMS2NCS_POLL_REQ__   0X011C
#define __NCS2NMS_POLL_ACK__   0X011D
#define __NCS2NMS_PARCFG_REQ__  0X011F
#define __NMS2NCS_PARCFG_ACK__  0X0120
#define __NCS2NMS_CFGRESULT_REPORT_REQ__   0X0121
#define __NMS2NCS_CFGRESULT_REPORT_ACK__   0X0122

/*The message types between NCS and PP*/
#define __PP2NCS_PARCFG_REQ__  0X0200
#define __NCS2PP_PARCFG_ACK__  0X0201
#define __PP2NCS_LOGON_REQ__  0X0202
#define __NCS2PP_LOGON_ACK__   0X0203
#define __PP2NCS_REPORT__    0X0204
#define __NCS2PP_PARCFG_UPDATE_REQ__  0X0205
#define __PP2NCS_PARCFG_UPDATE_ACK__  0X0206
#define __NCS2PP_QUERY_REQ__  0X0207
#define __PP2NCS_QUERY_ACK__  0X0208
#define __NCS2PP_POLL_REQ__	  0X0209
#define __PP2NCS_POLL_ACK__  0X020A
#define __PP2NCS_CFGRESULT_REPORT_REQ__   0X020C
#define __NCS2PP_CFGRESULT_REPORT_ACK__   0X020D

/*The message types between NCS and SIGEMS*/
#define __SIG2NCS_PARCFG_REQ__    0X0300
#define __NCS2SIG_PARCFG_ACK__    0X0301
#define __SIG2NCS_LOGON_REQ__   0X0302
#define __NCS2SIG_LOGON_ACK__    0X0303
#define __SIG2NCS_REPORT__   0X0304
#define __NCS2SIG_PARCFG_UPDATE_REQ__  0X0305
#define __SIG2NCS_PARCFG_UPDATE_ACK__  0X0306
#define __NCS2SIG_OPENCHA_REQ__  0X0307
#define __SIG2SIG_OPENCHA_ACK__   0X0308
#define __NCS2SIG_CLOSECHA_REQ__   0X0309
#define __SIG2SIG_CLOSECHA_ACK__   0X030A
#define __NCS2SIG_QUERY_REQ__   0X030B
#define __SIG2NCS_QUERY_ACK__  0X030C
#define __NCS2SIG_POLL_REQ__  0X030D
#define __SIG2NCS_POLL_ACK__   0X030E
#define __SIG2NCS_CFGRESULT_REPORT_REQ__   0X0310
#define __NCS2SIG_CFGRESULT_REPORT_ACK__   0X0311

/*The message types between NCS and RXEMS*/
#define __RX2NCS_PARCFG_REQ__      0X0400
#define __NCS2RX_PARCFG_ACK__      0X0401
#define __RX2NCS_LOGON_REQ__     0X0402
#define __NCS2RX_LOGON_ACK__    0X0403
#define __RX2NCS_REPORT__       0X0404
#define __NCS2RX_PARCFG_UPDATE_REQ__  0X0405
#define __RX2NCS_PARCFG_UPDATE_ACK__  0X0406
#define __NCS2RX_QUERY_REQ__  0X0407
#define __RX2NCS_QUERY_ACK__  0X0408
#define __NCS2RX_OPENRXCHA_REQ__   0X0409
#define __RX2NCS_OPENRXCHA_ACK__   0X040A
#define __NCS2RX_CLOSERXCHA_REQ__   0X040B
#define __RX2NCS_CLOSERXCHA_ACK__   0X040C
#define __NCS2RX_POLL_REQ__  0X040D
#define __RX2NCS_POLL_ACK__   0X040E
#define __RX2NCS_CFGRESULT_REPORT_REQ__   0X0410
#define __NCS2RX_CFGRESULT_REPORT_ACK__   0X0411

/*The message types between NCS and RCST*/
#define __RCST2NCS_PARCFG_REQ__   0X0500
#define __NCS2RCST_PARCFG_ACK__    0X0501
#define __RCST2NCS_LOGON_REQ__   0X0502
#define __NCS2RCST_LOGON_ACK__    0X0503
#define __RCST2NCS_REPORT__   0X0504
#define __NCS2RCST_PARCFG_UPDATE_REQ__  0X0505
#define __RCST2NCS_PARCFG_UPDATE_ACK__   0X0506
#define __NCS2RCST_QUERY_REQ__   0X0507
#define __RCST2NCS_QUERY_ACK__  0X0508
#define __NCS2RCST_RELEASE_RESOURCE_REQ__    0X0509
#define __RCST2NCS_RELEASE_RESOURCE_ACK__   0X050A
#define __RCST2NCS_RELEASE_RESOURCE_REQ__    0X050C
#define __NCS2RCST_RELEASE_RESOURCE_ACK__    0X050D
#define __NCS2RCST_CARRIERLOCK_CHA_REQ__   0X050F
#define __RCST2NCS_CARRIERLOCK_CHA_ACK__   0X0510
#define __NCS2RCST_CARRIERLOCK_RELEASE_REQ__   0X0511
#define __RCST2NCS_CARRIERLOCK_RELEASE_ACK__   0X0512
#define __RCST2NCS_LOGOFF_REQ__   0X0514
#define __NCS2RCST_LOGOFF_ACK__   0X0515
#define __NCS2RCST_LOGOFF_REQ__   0X0517
#define __RCST2NCS_LOGOFF_ACK__  0X0518
#define __RCST2NCS_RESOURCE_REQ__   0X051A
#define __NCS2RCST_RESOURCE_ACK__   0X051B
#define __RCST2NCS_BOD_REQ__  0X051C
#define __NCS2RCST_BOD_ACK__  0X051D
#define __RCST2NCS_BOD_RELEASE_REQ__  0X051E
#define __NCS2RCST_BOD_RELEASE_ACK__   0X051F
#define __NCS2RCST_POLL_REQ__  0X0521
#define __RCST2NCS_POLL_ACK__   0X0520
#define __RCST2NCS_CFGRESULT_REPORT_REQ__   0X0522
#define __NCS2RCST_CFGRESULT_REPORT_ACK__   0X0523

/*NCS Broadcast and sending IB information message types*/
#define __NCS_BROADCAST_IPPORT__	  0X0601
#define __NCS_SEND_IB_INFO__   0X0603


/*declaration functions*/
int get_seq(int *seq);
struct packet_ctl *build_packet_ctl(int size);
struct packet_ctl *find_packetctl(struct list *out,int seq);
struct packet_ctl * ncs_add_packetctl_list(struct dev_obj *dev,char *packet,int len);
struct packet_hdr *build_packet(int size);
void packet_free(void *packet);
int packet_compare_by_seq(void *val1,void *val2);
void ncs_sigserv_recv(int fd,short event,void *arg);
void ncs_poll_pp(int fd,short event,void *arg);
void ncs_poll_rx(int fd,short event,void *arg);
void ncs_poll_rcst(int fd,short event,void *arg);
void ncs_poll_sig(int fd,short event,void *arg);
int ncs2dev_send_cfg(struct ncs_object *ncs,struct dev_obj *dev,char *pinfo,int len);
int ncs_creat_serv(struct ncs_object *ncs);
int ncs_send_packet(int fd,int dip,short dport,char *buf,int len);
int ncs_start_broadcast(struct ncs_object *ncs);
void ncs_free_packetctl_list(struct dev_obj *dev);
int ncs2dev_send_routers_all(struct ncs_object *ncs,struct dev_obj *dev);
int ncs2dev_send_qosrules_all(struct ncs_object *ncs,struct dev_obj *dev);
int logon_pp2ncs_req(struct ncs_object *ncs,struct dev_obj *dev,void *arg,int len);
int ncs2pp_send_allcfg(struct ncs_object *ncs,struct dev_obj *dev);
int packet_router_tlv(char *pinfo,struct _STR_ADD_ROUTER_ *prouter,int size,char oper);
int packet_add_vlan_tlv(char *pinfo,struct _STR_VLAN_ *pvlan,int size);
int packet_qosrule_tlv(char *pinfo,struct _STR_ADD_QOSRULE_ITEMS_ *pqosrule,int size,char flag);
int packet_add_resource_tlv(char *pinfo,struct _STR_RESOURCE_ *presource,int len);
int packet_add_devname_tlv(char *pinfo,struct _STR_DEV_NAME_ *pdevname,int len);
int packet_add_carrier_tlv(char *pinfo,struct _STR_CARRIER_ *pcarrier,int len);
int packet_add_result_tlv(char *pinfo,struct _STR_RESULT_ *presult,int len);
int packet_add_devId_tlv(char *pinfo,struct _STR_DEV_ID_ *pdevId,int len);
int packet_router_tlv(char *pinfo,struct _STR_ADD_ROUTER_ *prouter,int len,char oper);
int packet_add_modem_tlv(char *pinfo,struct _STR_MODEM_ *pmodem,int len);
int packet_add_gwip_tlv(char *pinfo,struct _STR_DEFAULT_GWIP_ *pgwip,int len);
int packet_add_vlan_tlv(char *pinfo,struct _STR_VLAN_ *pvlan,int len);
int packet_add_chacomm_tlv(char *pinfo,struct _STR_CHANNEL_COMM_ *pchacomm,int len);
int packet_add_cha_tlv(char *pinfo,struct _STR_CHANNEL_ *pcha,int len);
int packet_qosrule_tlv(char *pinfo,struct _STR_ADD_QOSRULE_ITEMS_ *pqosrule,int len,char flag);
int packet_add_devtime_tlv(char *pinfo,struct _STR_DEV_TIME_ *pdevtime,int len);
int packet_add_workmode_tlv(char *pinfo,struct _STR_WORK_MODE_ *pworkmode,int len);
int packet_add_resourcebod_tlv(char *pinfo,struct _STR_RESOURCE_BOD_ *presourcebod,int len);
int packet_add_resourcerule_tlv(char *pinfo,struct _STR_RESOURCE_RULE_ITEMS_ *presourcerule,int len);
int packet_add_chaId_tlv(char *pinfo,struct _STR_CHANNEL_ID_ *pchaId,int len);
int modem_auth_check(struct dev_obj *dev,struct _STR_MODEM_ *pmodem,struct _STR_LICENSE_INFO_ *plicense);
void ncs_start_dev_poll_event(struct ncs_object *ncs,struct dev_obj *dev);
void ncs_start_send_Ib_event(struct ncs_object *ncs,struct dev_obj *dev);
void ncs2sig_send_IB(evutil_socket_t fd,short event,void *arg);
int ncs2dev_send_cfg(struct ncs_object *ncs,struct dev_obj *dev,char *pinfo,int len);
int add_retransmit_timer(struct ncs_object *ncs,struct packet_ctl *packet);
void ncs_proc_resend_max(struct packet_ctl *packetctl);
int ncs_rx_switch(int rxdevId,struct rcst_rx *prcst_rx,int *pnum);
void ncs_proc_modem_fault(struct ncs_object *ncs,struct dev_obj *dev,char fault);
void ncs2rcst_resend_max(struct packet_ctl *packetctl);
int ncs2sig_send_allcfg(struct ncs_object *ncs,struct dev_obj *sig);
void del_packetctl(struct list *out,struct packet_ctl *pctl);
int ncs2rx_openrxpath_req(struct ncs_object *ncs,struct dev_obj *rx,struct _STR_CHANNEL_ID_ *prxchaId);
int ncs2dev_logon_ack(struct ncs_object *ncs,struct dev_obj *dev,char val);
int ncs2rcst_resalloc_ack(struct ncs_object *ncs,struct dev_obj *rcst,char val);
int ncs2rcst_send_allcfg(struct ncs_object *ncs,struct dev_obj *rcst);
int ncs2rcst_locktxpath_req(struct ncs_object *ncs,struct dev_obj *rcst,struct _STR_CHANNEL_ID_ *ptxchaId,struct _STR_RESOURCE_ *presource);
int ncs2rcst_relres_req(struct ncs_object *ncs,struct dev_obj *rcst,struct _STR_CHANNEL_ID_ *ptxchaId);
int ncs2rcst_relres_ack(struct ncs_object *ncs,struct dev_obj *rcst,char val);
int ncs2rcst_logoff_ack(struct ncs_object *ncs,struct dev_obj *rcst,char val);
int ncs2rcst_logoff_req(struct ncs_object *ncs,struct dev_obj *rcst,struct _STR_CHANNEL_ID_ *ptxchaId);
int ncs2rcst_logon_ack(struct ncs_object *ncs,struct dev_obj *rcst,char val);
int logon_modem2ncs_req(struct ncs_object *ncs,struct dev_obj *dev,struct tlv_hdr *ptlv,int len);
int ncs2rcst_lockreltxpath_req(struct ncs_object *ncs,struct dev_obj *rcst,struct _STR_CHANNEL_ID_ *ptxchaId);
int ncs2rx_send_allcfg(struct ncs_object *ncs,struct dev_obj *rx);
int ncs2rx_logon_ack(struct ncs_object *ncs,struct dev_obj *rx,char val);
int ncs2sig_logon_ack(struct ncs_object *ncs,struct dev_obj *sig,char val);
int ncs2dev_send_cfg_01(struct ncs_object *ncs,struct dev_obj *dev);
int ncs2rx_closerxpath_req(struct ncs_object *ncs,struct dev_obj *rx,struct _STR_CHANNEL_ID_ *prxchaId);

#endif

